package reflect;

import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

public class Test {
    public static void main(String[] args) throws Exception {
        File dir = new File(Test.class.getResource(".").toURI());
        if(dir.isDirectory()){
            File[] subs = dir.listFiles(file -> file.getName().endsWith(".class"));
            System.out.println("当前目录下有"+subs.length+"个class文件");
            Class pname = Test.class;
            String packageName = pname.getPackage().getName();
            for(File sub : subs){
                String[] str = sub.getName().split("\\.");
                Class cls = Class.forName(packageName+"."+str[0]);
                Constructor con = cls.getConstructor();
                Object obj = con.newInstance();

               /* if(sub.getName().equals("Person.class")){
                    Constructor con1 = cls.getConstructor(String.class,int.class);
                    Object obj1 = con1.newInstance("王五", 20);
                    Method method = cls.getMethod("watchTV");
                    Object obj2 =method.invoke(obj1);
                }*/

                System.out.println(obj);
            }
        }

    }
}
